package com.hx.petsever.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hx.petsever.entity.User;
import com.hx.petsever.mapper.UserMapper;
import com.hx.petsever.service.UserService;
import lombok.extern.java.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    UserMapper userMapper;

    @Override
    public Map login(User user) {
        Map<String, Object> map = new HashMap<>();
        User user1 = userMapper.login(user.getPhone(), user.getPassword());
        if (user1 == null)
            return null;
        else {
            map.put("username", user1.getUsername());
            map.put("phone",user1.getPhone());
            return map;
        }
    }

    @Override
    public HashMap<String,String> Register(User user) {
        HashMap<String, String> map = new HashMap<>();
        //System.out.println(user.toString());
        if(userMapper.havaPhone(user.getPhone()) != null)
        {
            map.put("msg","该手机已注册");
            map.put("state","0");
        }
        else{
            String name = user.getPhone().substring(0,3)+"****"+user.getPhone().substring(7,11);
            userMapper.Register(name,user.getPassword(),user.getPhone());
            map.put("msg","注册成功！");
            map.put("state","1");
        }
        return map;
    }

    @Override
    public HashMap<String, String> verify(String password, String phone, String openID) {
        HashMap<String , String> map = new HashMap<>();
        if(userMapper.havaPhoneInVerify(phone) != null)
        {
            map.put("state","0");
            map.put("msg","该账户已绑定用户，请检查");
            return map;
        }
        if(userMapper.havaPhone(phone) != null)
        {
            User login = userMapper.login(phone, password);
            if(login == null)
            {
                map.put("msg","该用户已注册，密码错误！！");
                map.put("state","0");
            }else{
                userMapper.verify(phone,openID);
                map.put("msg","绑定成功！");
                map.put("state","1");
            }
            return map;
        }
        String name = phone.substring(0,3)+"****"+phone.substring(7,11);
        userMapper.Register(name,password,phone);
        userMapper.verify(phone,openID);
        map.put("msg","绑定成功");
        map.put("state","1");
        return map;
    }

    @Override
    public Boolean ifverify(String openID) {
        if(userMapper.havaOpenIdInVerify(openID) == null)
        {
            return false;
        }
        return true;
    }
}
